package com.yjq.programmer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjq.programmer.bean.CodeMsg;
import com.yjq.programmer.dao.AlbumMapper;
import com.yjq.programmer.dao.MusicMapper;
import com.yjq.programmer.dao.SingerMapper;
import com.yjq.programmer.domain.*;
import com.yjq.programmer.dto.PageDTO;
import com.yjq.programmer.dto.ResponseDTO;
import com.yjq.programmer.dto.SingerDTO;
import com.yjq.programmer.service.ISingerService;
import com.yjq.programmer.utils.CommonUtil;
import com.yjq.programmer.utils.CopyUtil;
import com.yjq.programmer.utils.UuidUtil;
import com.yjq.programmer.utils.ValidateEntityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨杨吖
 * @QQ 823208782
 * @WX yjqi12345678
 * @create 2022-11-14 11:22
 */
@Transactional
@Service
public class SingerServiceImpl implements ISingerService {

    @Resource
    private SingerMapper singerMapper;

    @Resource
    private MusicMapper musicMapper;

    @Resource
    private AlbumMapper albumMapper;

    /**
     * 分页获取歌手数据
     * @param pageDTO
     * @return
     */
    @Override
    public ResponseDTO<PageDTO<SingerDTO>> getSingerListByPage(PageDTO<SingerDTO> pageDTO) {
        SingerExample singerExample = new SingerExample();
        // 判断是否进行关键字搜索
        SingerDTO searchSingerDTO = pageDTO.getSearchEntity();
        SingerExample.Criteria criteria = singerExample.createCriteria();
        if(!CommonUtil.isEmpty(searchSingerDTO.getName())) {
            criteria.andNameLike("%" + searchSingerDTO.getName() + "%");
        }
        // 不知道当前页多少，默认为第一页
        if(pageDTO.getPage() == null){
            pageDTO.setPage(1);
        }
        if(pageDTO.getSize() == null) {
            pageDTO.setSize(5);
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        // 分页查出歌手数据
        List<Singer> singerList = singerMapper.selectByExample(singerExample);
        PageInfo<Singer> pageInfo = new PageInfo<>(singerList);
        // 获取数据的总数
        pageDTO.setTotal(pageInfo.getTotal());
        // 将domain类型数据  转成 DTO类型数据
        List<SingerDTO> singerDTOList = CopyUtil.copyList(singerList, SingerDTO.class);
        pageDTO.setList(singerDTOList);
        return ResponseDTO.success(pageDTO);
    }

    @Override
    public ResponseDTO<Boolean> saveSinger(SingerDTO singerDTO) {
        // 进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(singerDTO);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDTO.errorByMsg(validate);
        }
        Singer singer = CopyUtil.copy(singerDTO, Singer.class);
        if(CommonUtil.isEmpty(singer.getId())){
            // id为空 说明是添加数据
            // 判断歌手名称是否重复
            if(isSingerNameExist(singer, "")){
                return ResponseDTO.errorByMsg(CodeMsg.SINGER_NAME_EXIST);
            }
            // 生成8位id
            singer.setId(UuidUtil.getShortUuid());
            // 添加数据到数据库
            if(singerMapper.insertSelective(singer) == 0){
                return ResponseDTO.errorByMsg(CodeMsg.SINGER_ADD_ERROR);
            }
        }else{
            // id不为空 说明是修改数据
            // 判断歌手名称是否重复
            if(isSingerNameExist(singer, singer.getId())){
                return ResponseDTO.errorByMsg(CodeMsg.SINGER_NAME_EXIST);
            }
            // 修改数据库中数据
            if(singerMapper.updateByPrimaryKeySelective(singer) == 0){
                return ResponseDTO.errorByMsg(CodeMsg.SINGER_EDIT_ERROR);
            }
            Singer singerDB = singerMapper.selectByPrimaryKey(singer.getId());
            // 同步更新音乐中的歌手信息
            MusicExample musicExample = new MusicExample();
            musicExample.createCriteria().andSingerIdEqualTo(singerDB.getId());
            Music music = new Music();
            music.setName(singerDB.getName());
            musicMapper.updateByExampleSelective(music, musicExample);
            // 同步更新专辑中的歌手信息
            AlbumExample albumExample = new AlbumExample();
            albumExample.createCriteria().andSingerIdEqualTo(singerDB.getId());
            Album album = new Album();
            album.setSingerName(singerDB.getName());
            album.setSingerPhoto(singerDB.getHeadPic());
            albumMapper.updateByExampleSelective(album, albumExample);
        }
        return ResponseDTO.successByMsg(true, "保存成功！");
    }

    /**
     * 删除歌手数据
     * @param singerDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> removeSinger(SingerDTO singerDTO) {
        if(CommonUtil.isEmpty(singerDTO.getId())){
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 删除歌手信息
        if(singerMapper.deleteByPrimaryKey(singerDTO.getId()) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.SINGER_DELETE_ERROR);
        }
        return ResponseDTO.successByMsg(true, "删除成功！");
    }

    /**
     * 获取全部歌手数据
     * @return
     */
    @Override
    public ResponseDTO<List<SingerDTO>> getAllSinger() {
        SingerExample singerExample = new SingerExample();
        List<Singer> singerList = singerMapper.selectByExample(singerExample);
        return ResponseDTO.success(CopyUtil.copyList(singerList, SingerDTO.class));
    }

    /**
     * 搜素歌手数据
     * @param singerDTO
     * @return
     */
    @Override
    public ResponseDTO<List<SingerDTO>> searchSinger(SingerDTO singerDTO) {
        SingerExample singerExample = new SingerExample();
        if(!CommonUtil.isEmpty(singerDTO.getName())) {
            singerExample.createCriteria().andNameLike("%" + singerDTO.getName() + "%");
        }
        List<Singer> singerList = singerMapper.selectByExample(singerExample);
        return ResponseDTO.success(CopyUtil.copyList(singerList, SingerDTO.class));
    }

    /**
     * 根据歌手id获取歌手数据
     * @param singerDTO
     * @return
     */
    @Override
    public ResponseDTO<SingerDTO> getSingerById(SingerDTO singerDTO) {
        if(CommonUtil.isEmpty(singerDTO.getId())) {
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        Singer singer = singerMapper.selectByPrimaryKey(singerDTO.getId());
        return ResponseDTO.success(CopyUtil.copy(singer, SingerDTO.class));
    }

    /**
     * 获取全部歌手数据
     * @return
     */
    @Override
    public ResponseDTO<List<SingerDTO>> getAllSingerList() {
        SingerExample singerExample = new SingerExample();
        List<Singer> singerList = singerMapper.selectByExample(singerExample);
        return ResponseDTO.success(CopyUtil.copyList(singerList, SingerDTO.class));
    }

    /**
     * 增加歌手浏览量
     * @param singerDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> addSingerView(SingerDTO singerDTO) {
        if(CommonUtil.isEmpty(singerDTO.getId())) {
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        Singer singer = singerMapper.selectByPrimaryKey(singerDTO.getId());
        if(singer == null) {
            return ResponseDTO.errorByMsg(CodeMsg.SINGER_NOT_EXIST);
        }
        singer.setViewNum(singer.getViewNum() + 1);
        singerMapper.updateByPrimaryKeySelective(singer);
        return ResponseDTO.success(true);
    }

    /**
     * 获取热门歌手数据
     * @param pageDTO
     * @return
     */
    @Override
    public ResponseDTO<List<SingerDTO>> getHotSingerList(PageDTO<SingerDTO> pageDTO) {
        SingerExample singerExample = new SingerExample();
        singerExample.setOrderByClause("view_num desc");
        if(pageDTO.getSize() != null && pageDTO.getPage() != null) {
            PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        }
        List<Singer> singerList = singerMapper.selectByExample(singerExample);
        return ResponseDTO.success(CopyUtil.copyList(singerList, SingerDTO.class));
    }

    /**
     * 获取歌手总数
     * @return
     */
    @Override
    public ResponseDTO<Integer> getSingerTotal() {
        return ResponseDTO.success(singerMapper.countByExample(new SingerExample()));
    }

    /**
     * 判断歌手名称是否重复
     * @param singer
     * @param id
     * @return
     */
    public Boolean isSingerNameExist(Singer singer, String id) {
        SingerExample singerExample = new SingerExample();
        singerExample.createCriteria().andNameEqualTo(singer.getName());
        List<Singer> selectedSingerList = singerMapper.selectByExample(singerExample);
        if(selectedSingerList != null && selectedSingerList.size() > 0) {
            if(selectedSingerList.size() > 1){
                return true; //出现重名
            }
            if(!selectedSingerList.get(0).getId().equals(id)) {
                return true; //出现重名
            }
        }
        return false;//没有重名
    }
}
